package com.hxci.web.controller;

import com.hxci.web.bean.Conn;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;

public class BookServlet extends HttpServlet {
    ServletConfig servletConfig;
    @Override
    public void init(ServletConfig config) throws ServletException {
        servletConfig=config;

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        do_add(req,resp);
        String actionType = req.getParameter("actionType");
        out.println("bookType is :"+actionType);
        switch (actionType) {
            case "book_update":
                do_update(req, resp);
                break;
            case "book_add":
                do_add(req, resp);
                break;
            case "book_rent":
                do_rent(req, resp);
                break;
            default:
                out.println("error:"+actionType);
                break;
        }
    }

    private void do_update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");
        String bookid = req.getParameter("bookid");//图书ID
        String bookName = req.getParameter("bookName");//图书名称
        String bookType = req.getParameter("bookType");//图书类型
        String author = req.getParameter("author");//作者
        String date = req.getParameter("date");//出版日期




        String sql = "UPDATE books " +
                " SET bookName = ?, bookType = ?, author = ?, date = ? " +
                " WHERE bookid = ? ";
        List<Object> params = new ArrayList<>();
        params.add(bookName);
        params.add(bookType);
        params.add(author);
        params.add(date);
        params.add(bookid);

        Conn c=new Conn(realPath);
        c.doQuery(sql,params);
        resp.sendRedirect("book.jsp");
    }

    private void do_add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");


        String bookName = req.getParameter("bookName");
        String bookType = req.getParameter("bookType");
        String author = req.getParameter("author");
        String date = req.getParameter("date");
        String status="未借阅";

        String sql = "INSERT INTO books (  bookName, bookType, author, date,status)" +
                "VALUES" +
                "( ?,?,?,?,?);";
        List<Object> params = new ArrayList<>();
        params.add(bookName);
        params.add(bookType);
        params.add(author);
        params.add(date);
        params.add(status);

        Conn c=new Conn(realPath);
        c.doQuery(sql,params);
        //查询之前创造的bookid
        String sql2="select bookid from books where bookName=?";
        List<Object> params2=new ArrayList<>();
        params2.add(bookName);
        List<Map> pp=c.doQuery(sql2,params2);
        int id = Integer.parseInt(pp.get(0).get("bookid").toString());

        //在页面上显示输出成功
        String sql1 = "INSERT INTO borrowings (bookid,bookName, status) VALUES (?,?, ?)";


        List<Object> paramsl = new ArrayList<>();
        paramsl.add(id);
        paramsl.add(bookName);
        paramsl.add(status);
        c.doQuery(sql1,paramsl);
        resp.sendRedirect("book.jsp");

    }
    private void do_rent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");
        Conn c=new Conn(realPath);
        out.println("正在借阅中") ;
        String bookid=req.getParameter("bookid");

        String readername = (String) req.getSession().getAttribute("loginc");

        //获取今天日期并格式化，如2025-10-01
        String Borrowingtime= LocalDate.now().format(DateTimeFormatter.ISO_DATE);
        //归还时间为借阅时间加7天
        LocalDate returntime1 = LocalDate.parse(Borrowingtime);
        LocalDate newDate = returntime1.plusDays(7);
        String newDateString = newDate.format(DateTimeFormatter.ISO_DATE);
        String status ="已借阅";
        String sql = "UPDATE borrowings SET readername = ?, Borrowingtime = ?, returntime = ?, status = ?,phone=(select phone from readers where readername=?) WHERE bookid = ? ";
        List<Object> params = new ArrayList<>();
        params.add(readername);
        params.add(Borrowingtime);
        params.add(newDateString);
        params.add(status);
        params.add(readername);
        params.add(bookid);
        System.out.println(":"+params);
        c.doQuery(sql,params);

        String sql2="UPDATE books SET status = ?,phone=(select phone from user where login_account=?) WHERE bookid = ? ";
        List<Object> params2 = new ArrayList<>();
        params2.add(status);
        params2.add(bookid);
        c.doQuery(sql2,params2);
        resp.sendRedirect("book1.jsp");

    }
}
